package com.samehope.ar.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import com.samehope.ar.service.*;
import com.samehope.ar.vo.PatrolTaskPlanWeekDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 根据计划任务 生成正式任务
 * </p>
 *
 * @author LD
 * @since 2020/1/8
 */
@Slf4j
@Component
public class TaskCreateService {

    @Autowired
    private ITaskNormalService taskNormalService;

    @Autowired
    private IPatrolTaskPlanService patrolTaskPlanService;

    @Autowired
    private ITaskRouteService taskRouteService;

    @Autowired
    private ITaskPointService taskPointService;

    @Autowired
    private ITaskItemService taskItemService;

    @Autowired
    private ITaskItemChildService taskItemChildService;

    @Scheduled(cron = "0 41 16 ? * THU")
    public void scheduled() {
        log.info("=====>>>>>TaskCreateService  {}", System.currentTimeMillis());

        //获取待生成计划任务信息
        List<PatrolTaskPlanWeekDetailVO> patrolTaskPlans = patrolTaskPlanService.findByEffect();

        //删除下周正式任务
        String nextWeek = DateUtil.format(DateUtil.beginOfWeek(DateUtil.nextWeek()), "yyyy-MM-dd");
        taskNormalService.delByTaskTime(nextWeek);
        taskRouteService.delByTaskTime(nextWeek);
        taskPointService.delByTaskTime(nextWeek);
        taskItemService.delByTaskTime(nextWeek);
        taskItemChildService.delByTaskTime(nextWeek);

        patrolTaskPlans.forEach(patrolTaskPlan -> {

            //插入正式执行任务
            taskNormalService.insertByPlans(patrolTaskPlan);

        });

        //插入线路快照
        taskRouteService.insertAll();

        //插入点位快照
        taskPointService.insertAll();

        //插入项目快照
        taskItemService.insertAll();

        //插入巡检项子表快照
        taskItemChildService.insertAll();
    }
}
